package mytest.ssw.www.api;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import mytest.ssw.www.mylibrary.Contents;
import mytest.ssw.www.mylibrary.DeptNode;
import mytest.ssw.www.mylibrary.StudentInfo;

/**
 * Created by hasee on 2016/5/2.
 */
public class SqliteTools<T> {
    static SQLiteDatabase db;
  //  String FilesDir;
    private static final  String DB_NAME = "/kmuCampus.db3";
    String DB;

    public SqliteTools() {
//        SqliteTools.class.getClassLoader().getResource("");
//        Log.d("sqLiteTools", SqliteTools.class.getClassLoader().getResource("").getPath());
        db = SQLiteDatabase.openOrCreateDatabase(DB_NAME, null);
        Log.d("sqLiteToolsFilesDirqq", db.toString());
    }

    public SqliteTools(final String FilesDir) {
        Log.d("sqLiteToolsDir_FileDir", ""+FilesDir);
        Log.d("sqLiteToolsDir", ""+DB);
         DB = FilesDir+DB_NAME;
        db = SQLiteDatabase.openOrCreateDatabase(DB, null);
        Log.d("sqLiteToolsFilesDir", db.toString());

    }

    public SQLiteDatabase getDB() {
        return this.db;
    }

    public void initDB(String sql) {
//        String dataPath =
        //Log.d("sqLiteTools3", dataPath);
        db = SQLiteDatabase.openOrCreateDatabase(DB, null);
        db.execSQL(sql);
    }

    public List<T> queryBySql(Class modelType, String querySql) {
        db = SQLiteDatabase.openOrCreateDatabase(DB, null);
        List<T> dataList = new ArrayList<T>();
        if (querySql == null || "".equals(querySql)) {
            return dataList;
        } else {
            Cursor cursor = db.rawQuery(querySql, null);
            cursor.moveToFirst();
            for (int i = 0; i < cursor.getCount(); i++) {
                try {
                    T model = (T) modelType.newInstance();
                    for (int j = 0; j < cursor.getColumnCount(); j++) {
                        String fieldName = cursor.getColumnName(j);
                        Log.i("www", "eee");
                        Log.i(cursor.getColumnName(j).toString(), cursor.getString(j));
                        Method[] methods = modelType.getMethods();
                        for (int k = 0; k < methods.length; k++) {
                            if (methods[k].getName().toLowerCase().equals("set" + fieldName.toLowerCase())) {
                                String val = cursor.getString(j);
                                if (val != null && !"".equals(val)) {
                                    String returntype = methods[k].getParameterTypes()[0].getSimpleName().toLowerCase();
                                    Log.i("qqqw", returntype);
                                    Object oval = null;
                                    if (returntype.equals("string")) {
                                        oval = val;
                                    } else if (returntype.equals("character")) {
                                        oval = (val.charAt(0));
                                    } else if (returntype.equals("boolean")) {
                                        oval = val.equals("1") ? true : false;
                                    } else if (returntype.equals("integer") || returntype.equals("int")) {
                                        oval = Integer.parseInt(val);
                                    } else if (returntype.equals("byte")) {
                                        oval = Byte.parseByte(val);
                                    } else if (returntype.equals("short")) {
                                        oval = Short.parseShort(val);
                                    } else if (returntype.equals("double")) {
                                        oval = Double.parseDouble(val);
                                    } else if (returntype.equals("float")) {
                                        oval = Float.parseFloat(val);
                                    } else if (returntype.equals("long")) {
                                        oval = Long.parseLong(val);
                                    }
                                    methods[k].invoke(model, oval);
                                }
                            }
                        }
                    }

                    dataList.add(model);

                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                cursor.moveToNext();
            }
        }
        return dataList;
    }


    public Cursor initSelectCursor(String tbName, String condition) {
        Cursor cursor;
        if (condition != null) {
            if (condition.contains("BM")) {
                if (tbName == "TB_DEPT" && condition.contains("BM")) {
                    condition = "  where num='" + condition + "'";
                } else {
                    Log.d("initSelectCursor1", "------" + tbName + "-----" + condition);
                    condition = "  where departmentNum like  '" + condition + "%'";
                }
            } else {
                condition = "  where num=" + condition;
            }
        } else {
            condition = "";
        }
        Log.d("Select tbName", "-----" + tbName);
        if (tbName != null) {
            try {
                final String sql = "select * from " + tbName + condition + "  order by _id DESC";
                Log.d("Select SQL", "-----" + sql);
                cursor = db.rawQuery(sql, null);
                Log.d("rawQuery", "------" + tbName + "-----" + condition + "-----" + cursor.getCount());
            } catch (SQLiteException se) {
                //执行DDL创建数据表
                //       db.execSQL("drop table if exists  "+tbName);
                if (tbName.contains("STU")) {     //执行创建学生表语句
                    db.execSQL("create table IF NOT EXISTS " + tbName + "(" +
                            " _id  integer primary key autoincrement," +
                            "  id varchar(50) NOT NULL," +
                            "  num varchar(20) NOT NULL," +
                            "  name varchar(10) DEFAULT NULL," +
                            "  className varchar(10) DEFAULT NULL," +
                            "  departmentNum varchar(50) DEFAULT NULL," +
                            "  gender varchar(10) DEFAULT NULL," +
                            "  dormitory varchar(20) DEFAULT NULL," +
                            "  photoPath varchar(50) DEFAULT NULL," +
                            "  phoneNum varchar(20) DEFAULT NULL," +
                            "  imeinum varchar(30) DEFAULT NULL," +
                            "  nation varchar(10) DEFAULT NULL," +
                            "  politicalStatus varchar(10) DEFAULT NULL," +
                            "  birthday DEFAULT NULL," +
                            "  birthplace varchar(50) DEFAULT NULL," +
                            "  parentName varchar(10) DEFAULT NULL," +
                            "  parentPhone varchar(20) DEFAULT NULL," +
                            "  homeAddress varchar(50) DEFAULT NULL," +
                            "  postCode varchar(10) DEFAULT NULL," +
                            "  qq varchar(20) DEFAULT NULL," +
                            "  valid bit(1) DEFAULT NULL" + ")");
                    Log.i("Create TABLE", "--------suc-----" + tbName);
                }
                if (tbName.contains("DEPT"))     //执行创建部门表语句
                {
                    db.execSQL("create table IF NOT EXISTS  " + tbName + "( " +
                                    "   _id  integer primary key autoincrement," +
                                    "   id  varchar(50) NOT NULL," +
                                    "   num  varchar(50) DEFAULT NULL, " +
                                    "   name  varchar(20) DEFAULT NULL, " +
                                    "   fullName  varchar(100) DEFAULT NULL, " +
                                    "   pNum  varchar(50) DEFAULT NULL, " +
                                    "   level  int(1) DEFAULT NULL, " +
                                    "   leaf  int(1) DEFAULT NULL, " +
                                    "   orderNum  varchar(15) DEFAULT NULL, " +
                                    "   valid  int(1) DEFAULT NULL " +
                                    ")"
                    );

                }
                //执行查询
                if (tbName.contains("DEPT"))
                    cursor = db.rawQuery("select * from " + tbName + condition + "  order by _id DESC", null);
                else
                    cursor = db.rawQuery("select * from " + tbName + condition + "  order by _id DESC", null);
                Log.i("SqLite_initSelectCursor", "--------suc-----" + tbName);
            }
        } else {
            cursor = null;
        }

        return cursor;
    }

    public long insertStuInfo(String tbName, StudentInfo stuInfo) {
        long ret = 0;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd    HH:mm:ss     ");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String dateStr = formatter.format(curDate);
        Log.d("insertStuInfo", dateStr);
        //实例化常量值
        if (initSelectCursor(tbName, null) != null) {
            ContentValues cValue = new ContentValues();
            if (stuInfo.getNum() != null) cValue.put("num", stuInfo.getNum());
            if (stuInfo.getName() != null) cValue.put("name", stuInfo.getName());
            if (stuInfo.getBirthday() != null) cValue.put("birthday", stuInfo.getBirthday());
            if (stuInfo.getBirthplace() != null) cValue.put("birthplace", stuInfo.getBirthplace());
            if (stuInfo.getDepartmentNum() != null)
                cValue.put("departmentNum", stuInfo.getDepartmentNum());
            if (stuInfo.getDormitory() != null) cValue.put("dormitory", stuInfo.getDormitory());
            if (stuInfo.getGender() != null) cValue.put("gender", stuInfo.getGender());
            if (stuInfo.getHomeAddress() != null)
                cValue.put("homeAddress", stuInfo.getHomeAddress());
            if (stuInfo.getId() != null) cValue.put("id", stuInfo.getId());
            if (stuInfo.getNation() != null) cValue.put("Nation", stuInfo.getNation());
            if (stuInfo.getNation() != null) cValue.put("imeinum", stuInfo.getImeinum());
            if (stuInfo.getParentName() != null) cValue.put("parentName", stuInfo.getParentName());
            if (stuInfo.getParentPhone() != null)
                cValue.put("parentPhone", stuInfo.getParentPhone());
            if (stuInfo.getPhoneNum() != null) cValue.put("phoneNum", stuInfo.getPhoneNum());
            if (stuInfo.getPhotoPath() != null) cValue.put("photoPath", stuInfo.getPhotoPath());
            if (stuInfo.getPoliticalStatus() != null)
                cValue.put("politicalStatus", stuInfo.getPoliticalStatus());
            if (stuInfo.getPostCode() != null) cValue.put("postCode", stuInfo.getPostCode());
            if (stuInfo.getValid() != null) cValue.put("valid", stuInfo.getValid());
            if (stuInfo.getQq() != null) cValue.put("qq", stuInfo.getQq());
            //调用insert()方法插入数据
            ret = db.insert(tbName, null, cValue);
            Log.d("insertStuInfo", "--------suc");
        }

        return ret;
    }

    public List<StudentInfo> getStuInfoFrDB(String tbName, String account) {
        List<StudentInfo> rst = new ArrayList<>();
        Cursor cursor = initSelectCursor(tbName, account);
        cursor.moveToFirst();
        boolean flag = true;
        for (int i = 0; i < cursor.getCount(); i++) {
            StudentInfo stuInfo = new StudentInfo();
            Log.d("getDeptfoFrDB", Integer.toString(cursor.getCount()));
            if (cursor.getString(cursor.getColumnIndex("id")) != null)
                stuInfo.setId(cursor.getString(cursor.getColumnIndex("id")));
            if (cursor.getString(cursor.getColumnIndex("num")) != null)
                stuInfo.setNum(cursor.getString(cursor.getColumnIndex("num")));

            if (cursor.getString(cursor.getColumnIndex("name")) != null)
                stuInfo.setName(cursor.getString(cursor.getColumnIndex("name")));
            Log.d("getStuInfoFrDB:", "cursor.num" + cursor.getString(cursor.getColumnIndex("name")) + "-----" + "stuInfo:" + stuInfo.getName());
            if (cursor.getString(cursor.getColumnIndex("nation")) != null)
                stuInfo.setNation(cursor.getString(cursor.getColumnIndex("nation")));
            if (cursor.getString(cursor.getColumnIndex("gender")) != null)
                stuInfo.setGender(cursor.getString(cursor.getColumnIndex("gender")));
            if (cursor.getString(cursor.getColumnIndex("birthday")) != null)
                stuInfo.setBirthday(cursor.getString(cursor.getColumnIndex("birthday")));
            if (cursor.getString(cursor.getColumnIndex("birthplace")) != null)
                stuInfo.setBirthplace(cursor.getString(cursor.getColumnIndex("birthplace")));
            if (cursor.getString(cursor.getColumnIndex("homeAddress")) != null)
                stuInfo.setHomeAddress(cursor.getString(cursor.getColumnIndex("homeAddress")));
            if (cursor.getString(cursor.getColumnIndex("departmentNum")) != null)
                stuInfo.setDepartmentNum(cursor.getString(cursor.getColumnIndex("departmentNum")));
            if (cursor.getString(cursor.getColumnIndex("dormitory")) != null)
                stuInfo.setDormitory(cursor.getString(cursor.getColumnIndex("dormitory")));
            if (cursor.getString(cursor.getColumnIndex("politicalStatus")) != null)
                stuInfo.setPoliticalStatus(cursor.getString(cursor.getColumnIndex("politicalStatus")));
            if (cursor.getString(cursor.getColumnIndex("postCode")) != null)
                stuInfo.setPostCode(cursor.getString(cursor.getColumnIndex("postCode")));
            if (cursor.getString(cursor.getColumnIndex("parentName")) != null)
                stuInfo.setParentName(cursor.getString(cursor.getColumnIndex("parentName")));
            if (cursor.getString(cursor.getColumnIndex("parentPhone")) != null)
                stuInfo.setParentPhone(cursor.getString(cursor.getColumnIndex("parentPhone")));
            if (cursor.getString(cursor.getColumnIndex("phoneNum")) != null)
                stuInfo.setPhoneNum(cursor.getString(cursor.getColumnIndex("phoneNum")));
            if (cursor.getString(cursor.getColumnIndex("photoPath")) != null)
                stuInfo.setPhotoPath(cursor.getString(cursor.getColumnIndex("photoPath")));
            if (cursor.getString(cursor.getColumnIndex("qq")) != null)
                stuInfo.setQq(cursor.getString(cursor.getColumnIndex("qq")));
            if (cursor.getString(cursor.getColumnIndex("valid")) != null)
                stuInfo.setValid(cursor.getString(cursor.getColumnIndex("valid")));
            if (stuInfo != null) rst.add(stuInfo);
            flag = cursor.moveToNext();
            Log.d("getStuInfoFrDB", "-------suc");
        }
        return rst;
    }

    public void insertDetp(String tbName, List<DeptNode> deptNodeList) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd    HH:mm:ss     ");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String dateStr = formatter.format(curDate);
        Log.d("insertStuInfo", dateStr);
        //实例化常量值
        for (int i = 0; i < deptNodeList.size(); i++) {
            DeptNode dept = deptNodeList.get(i);
            ContentValues cValue = new ContentValues();
            if (dept.getId() != null) cValue.put("id", dept.getId());
            if (dept.getNum() != null) cValue.put("num", dept.getNum());
            if (dept.getName() != null) cValue.put("name", dept.getName());
            if (dept.getFullName() != null) cValue.put("fullName", dept.getFullName());
            if (dept.getpNum() != null) cValue.put("pNum", dept.getpNum());
            cValue.put("level", dept.getLevel());
            cValue.put("orderNum", dept.getOrderNum());
            if (dept.isLeaf()) cValue.put("leaf", 1);
            else cValue.put("leaf", 0);
            if (dept.isValid()) cValue.put("valid", 1);
            else cValue.put("valid", 0);
            cValue.put("valid", dept.isValid());
            //调用insert()方法插入数据
            if (curDate != null)
                db.insert(tbName, null, cValue);
        }
    }

    public List<DeptNode> getDeptfoFrDB(String tbName) {
        List<DeptNode> rst = new ArrayList<>();
        Cursor cursor = initSelectCursor(tbName, null);
        cursor.moveToFirst();
        boolean flag = true;
        for (int i = 0; i < cursor.getCount(); i++) {
            DeptNode dept = new DeptNode();
            Log.d("getDeptfoFrDB", Integer.toString(cursor.getCount()));
            if (cursor.getString(cursor.getColumnIndex("id")) != null)
                dept.setId(cursor.getString(cursor.getColumnIndex("id")));
            if (cursor.getString(cursor.getColumnIndex("num")) != null)
                dept.setNum(cursor.getString(cursor.getColumnIndex("num")));
            if (cursor.getString(cursor.getColumnIndex("name")) != null)
                dept.setName(cursor.getString(cursor.getColumnIndex("name")));
            if (cursor.getString(cursor.getColumnIndex("pNum")) != null)
                dept.setpNum(cursor.getString(cursor.getColumnIndex("pNum")));
            if (cursor.getString(cursor.getColumnIndex("fullName")) != null)
                dept.setFullName(cursor.getString(cursor.getColumnIndex("fullName")));
            dept.setLevel(cursor.getInt(cursor.getColumnIndex("level")));
            dept.setOrderNum(cursor.getInt(cursor.getColumnIndex("orderNum")));
            if (dept != null) rst.add(dept);
            flag = cursor.moveToNext();
            Log.d("getStuInfoFrDB", i + "-------suc");
        }
        return rst;
    }

    public List<DeptNode> getDeptByNum(String tbName, String BmNum) {
        List<DeptNode> rst = new ArrayList<>();
        Cursor cursor = initSelectCursor(tbName, BmNum);
        cursor.moveToFirst();
        boolean flag = true;
        for (int i = 0; i < cursor.getCount(); i++) {
            DeptNode dept = new DeptNode();
            Log.d("getDeptfoFrDB", Integer.toString(cursor.getCount()));
            if (cursor.getString(cursor.getColumnIndex("id")) != null)
                dept.setId(cursor.getString(cursor.getColumnIndex("id")));
            if (cursor.getString(cursor.getColumnIndex("num")) != null)
                dept.setNum(cursor.getString(cursor.getColumnIndex("num")));
            if (cursor.getString(cursor.getColumnIndex("name")) != null)
                dept.setName(cursor.getString(cursor.getColumnIndex("name")));
            if (cursor.getString(cursor.getColumnIndex("pNum")) != null)
                dept.setpNum(cursor.getString(cursor.getColumnIndex("pNum")));
            if (cursor.getString(cursor.getColumnIndex("fullName")) != null)
                dept.setFullName(cursor.getString(cursor.getColumnIndex("fullName")));
            dept.setLevel(cursor.getInt(cursor.getColumnIndex("level")));
            dept.setOrderNum(cursor.getInt(cursor.getColumnIndex("orderNum")));
            if (dept != null) rst.add(dept);
            flag = cursor.moveToNext();
            Log.d("getStuInfoFrDB", i + "-------suc");
        }
        return rst;
    }

    public boolean cleanTB(String tbName) {
        boolean flag = false;
        try {
            this.getDB().execSQL("drop table if exists  " + tbName);
            flag = true;
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    public long insertTeacherInfo(String tbName, Contents stuInfo) {
        long ret = 0;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd    HH:mm:ss     ");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String dateStr = formatter.format(curDate);
        Log.d("insertStuInfo", dateStr);
        //实例化常量值
        if (initSelectCursor(tbName, null) != null) {
            ContentValues cValue = new ContentValues();
            if (stuInfo.getClassName() != null) cValue.put("className", stuInfo.getClassName());
            if (stuInfo.getNum() != null) cValue.put("num", stuInfo.getNum());
            if (stuInfo.getName() != null) cValue.put("name", stuInfo.getName());
            if (stuInfo.getBirthday() != null) cValue.put("birthday", stuInfo.getBirthday());
            if (stuInfo.getBirthplace() != null) cValue.put("birthplace", stuInfo.getBirthplace());
            if (stuInfo.getDepartmentNum() != null)
                cValue.put("departmentNum", stuInfo.getDepartmentNum());
            if (stuInfo.getDormitory() != null) cValue.put("dormitory", stuInfo.getDormitory());
            if (stuInfo.getGender() != null) cValue.put("gender", stuInfo.getGender());
            if (stuInfo.getHomeAddress() != null)
                cValue.put("homeAddress", stuInfo.getHomeAddress());
            if (stuInfo.getId() != null) cValue.put("id", stuInfo.getId());
            if (stuInfo.getNation() != null) cValue.put("Nation", stuInfo.getNation());
            if (stuInfo.getNation() != null) cValue.put("imeinum", stuInfo.getImeinum());
            if (stuInfo.getParentName() != null) cValue.put("parentName", stuInfo.getParentName());
            if (stuInfo.getParentPhone() != null)
                cValue.put("parentPhone", stuInfo.getParentPhone());
            if (stuInfo.getPhoneNum() != null) cValue.put("phoneNum", stuInfo.getPhoneNum());
            if (stuInfo.getPhotoPath() != null) cValue.put("photoPath", stuInfo.getPhotoPath());
            if (stuInfo.getPoliticalStatus() != null)
                cValue.put("politicalStatus", stuInfo.getPoliticalStatus());
            if (stuInfo.getPostCode() != null) cValue.put("postCode", stuInfo.getPostCode());
            if (stuInfo.getValid() != null) cValue.put("valid", stuInfo.getValid());
            if (stuInfo.getQq() != null) cValue.put("qq", stuInfo.getQq());
            //调用insert()方法插入数据
            ret = db.insert(tbName, null, cValue);
            Log.d("insertTeacherInfo", "--------suc");
        }

        return ret;
    }

    public long insertTeacherInfo(String tbName, List<Contents> contentsList) {
        long ret = 0;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd    HH:mm:ss     ");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String dateStr = formatter.format(curDate);
        Log.d("insertTeacherInfo", "---" + contentsList.size() + "----" + contentsList.get(0).getDepartmentNum());
        for (int i = 0; i < contentsList.size(); i++) {
            Contents stuInfo = contentsList.get(i);
            //实例化常量值
            if (initSelectCursor(tbName, null) != null) {
                ContentValues cValue = new ContentValues();
                if (stuInfo.getClassName() != null) cValue.put("className", stuInfo.getClassName());
                if (stuInfo.getNum() != null) cValue.put("num", stuInfo.getNum());
                if (stuInfo.getName() != null) cValue.put("name", stuInfo.getName());
                if (stuInfo.getBirthday() != null) cValue.put("birthday", stuInfo.getBirthday());
                if (stuInfo.getBirthplace() != null)
                    cValue.put("birthplace", stuInfo.getBirthplace());
                if (stuInfo.getDepartmentNum() != null)
                    cValue.put("departmentNum", stuInfo.getDepartmentNum());
                if (stuInfo.getDormitory() != null) cValue.put("dormitory", stuInfo.getDormitory());
                if (stuInfo.getGender() != null) cValue.put("gender", stuInfo.getGender());
                if (stuInfo.getHomeAddress() != null)
                    cValue.put("homeAddress", stuInfo.getHomeAddress());
                if (stuInfo.getId() != null) cValue.put("id", stuInfo.getId());
                if (stuInfo.getNation() != null) cValue.put("Nation", stuInfo.getNation());
                if (stuInfo.getNation() != null) cValue.put("imeinum", stuInfo.getImeinum());
                if (stuInfo.getParentName() != null)
                    cValue.put("parentName", stuInfo.getParentName());
                if (stuInfo.getParentPhone() != null)
                    cValue.put("parentPhone", stuInfo.getParentPhone());
                if (stuInfo.getPhoneNum() != null) cValue.put("phoneNum", stuInfo.getPhoneNum());
                if (stuInfo.getPhotoPath() != null) cValue.put("photoPath", stuInfo.getPhotoPath());
                if (stuInfo.getPoliticalStatus() != null)
                    cValue.put("politicalStatus", stuInfo.getPoliticalStatus());
                if (stuInfo.getPostCode() != null) cValue.put("postCode", stuInfo.getPostCode());
                if (stuInfo.getValid() != null) cValue.put("valid", stuInfo.getValid());
                if (stuInfo.getQq() != null) cValue.put("qq", stuInfo.getQq());
                //调用insert()方法插入数据
                ret = db.insert(tbName, null, cValue);
                Log.d("insertTeacherInfo", "--------suc");
            }
        }
        return ret;
    }

    public List<Contents> getTeacherInfoFrDB(String tbName, String deptNum) {
        List<Contents> rst = new ArrayList<>();
        Log.d("getTeacherInfoFrDB", "------" + tbName + "-----" + deptNum);
        Cursor cursor = initSelectCursor(tbName, deptNum);
        Log.d("getTeacherInfoFrDB", "------" + tbName + "-----" + deptNum + "-----" + cursor.getCount());
        cursor.moveToFirst();
        boolean flag = true;
        for (int i = 0; i < cursor.getCount(); i++) {
            Contents stuInfo = new Contents();
            Log.d("getTeacherInfoFrDB", "------" + tbName + "-----" + deptNum);
            if (cursor.getString(cursor.getColumnIndex("className")) != null)
                stuInfo.setClassName(cursor.getString(cursor.getColumnIndex("className")));
            if (cursor.getString(cursor.getColumnIndex("id")) != null)
                stuInfo.setId(cursor.getString(cursor.getColumnIndex("id")));
            if (cursor.getString(cursor.getColumnIndex("num")) != null)
                stuInfo.setNum(cursor.getString(cursor.getColumnIndex("num")));
            if (cursor.getString(cursor.getColumnIndex("name")) != null)
                stuInfo.setName(cursor.getString(cursor.getColumnIndex("name")));
            Log.d("getTeacherInfoFrDB:", "cursor.num" + cursor.getString(cursor.getColumnIndex("name")) + "-----" + "stuInfo:" + stuInfo.getName());
            if (cursor.getString(cursor.getColumnIndex("nation")) != null)
                stuInfo.setNation(cursor.getString(cursor.getColumnIndex("nation")));
            if (cursor.getString(cursor.getColumnIndex("gender")) != null)
                stuInfo.setGender(cursor.getString(cursor.getColumnIndex("gender")));
            if (cursor.getString(cursor.getColumnIndex("birthday")) != null)
                stuInfo.setBirthday(cursor.getString(cursor.getColumnIndex("birthday")));
            if (cursor.getString(cursor.getColumnIndex("birthplace")) != null)
                stuInfo.setBirthplace(cursor.getString(cursor.getColumnIndex("birthplace")));
            if (cursor.getString(cursor.getColumnIndex("homeAddress")) != null)
                stuInfo.setHomeAddress(cursor.getString(cursor.getColumnIndex("homeAddress")));
            if (cursor.getString(cursor.getColumnIndex("departmentNum")) != null)
                stuInfo.setDepartmentNum(cursor.getString(cursor.getColumnIndex("departmentNum")));
            if (cursor.getString(cursor.getColumnIndex("dormitory")) != null)
                stuInfo.setDormitory(cursor.getString(cursor.getColumnIndex("dormitory")));
            if (cursor.getString(cursor.getColumnIndex("politicalStatus")) != null)
                stuInfo.setPoliticalStatus(cursor.getString(cursor.getColumnIndex("politicalStatus")));
            if (cursor.getString(cursor.getColumnIndex("postCode")) != null)
                stuInfo.setPostCode(cursor.getString(cursor.getColumnIndex("postCode")));
            if (cursor.getString(cursor.getColumnIndex("parentName")) != null)
                stuInfo.setParentName(cursor.getString(cursor.getColumnIndex("parentName")));
            if (cursor.getString(cursor.getColumnIndex("parentPhone")) != null)
                stuInfo.setParentPhone(cursor.getString(cursor.getColumnIndex("parentPhone")));
            if (cursor.getString(cursor.getColumnIndex("phoneNum")) != null)
                stuInfo.setPhoneNum(cursor.getString(cursor.getColumnIndex("phoneNum")));
            if (cursor.getString(cursor.getColumnIndex("photoPath")) != null)
                stuInfo.setPhotoPath(cursor.getString(cursor.getColumnIndex("photoPath")));
            if (cursor.getString(cursor.getColumnIndex("qq")) != null)
                stuInfo.setQq(cursor.getString(cursor.getColumnIndex("qq")));
            if (cursor.getString(cursor.getColumnIndex("valid")) != null)
                stuInfo.setValid(cursor.getString(cursor.getColumnIndex("valid")));
            if (stuInfo != null) {
                Log.d("getTeacherInfoFrDB_RST", "------" + tbName + "-----" + deptNum);
                rst.add(stuInfo);
            }
            flag = cursor.moveToNext();
        }
        Log.d("getTeacherInfoFrDB", "-------suc");
        return rst;
    }

    public String getRecordCount(String tbName) {
        String sql = "select _id as COUNT from  " + tbName;
        Cursor cs = getDB().rawQuery(sql, null);
        int count = cs.getCount();
        return Integer.toString(count);
    }
}